<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="消息标题" prop="noticeTitle">
        <el-input v-model="queryParams.noticeTitle" placeholder="请输入消息标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="消息类型" prop="noticeType">
        <el-select v-model="queryParams.noticeType" placeholder="请选择消息类型">
          <el-option label="站内消息" :value="1" />
          <el-option label="系统通知" :value="2" />
          <el-option label="其他消息" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:message:add']">
          新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="messageList" border>
      <el-table-column label="消息标题" align="center" prop="noticeTitle" />
      <el-table-column label="消息类型" align="center" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.noticeType == 1">站内消息</span>
          <span v-if="scope.row.noticeType == 2">系统通知</span>
          <span v-if="scope.row.noticeType == 3">其他消息</span>
        </template>
      </el-table-column>
      <el-table-column label="消息内容" align="center" prop="noticeContent">
        <template slot-scope="scope">
          <span v-html="scope.row.noticeContent"></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:message:remove']">
            删除
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDetials(scope.row)"
            v-hasPermi="['system:message:remove']">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.params.pageNum"
      :limit.sync="queryParams.params.pageSize" @pagination="getList" />

    <!-- 添加或修改站内消息 对话框 -->
    <el-dialog :title="form.noticeId ? '编辑' : '新增'" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="消息标题" prop="noticeTitle">
          <el-input v-model="form.noticeTitle" placeholder="请输入消息标题" />
        </el-form-item>
        <el-form-item label="消息内容">
          <Editors v-model="form.noticeContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="通知人员" prop="member">
          <el-select v-model="form.userIds" multiple placeholder="请选择" clearable>
            <el-option v-for="item in memberListOptions" :key="item.userId" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消息类型" prop="member">
          <el-select v-model="form.noticeType" placeholder="请选择" clearable>
            <el-option v-for="item in noticeTypeOptions" :key="item.userId" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submiting" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog title="详情" :visible.sync="detialsOpen" width="50%" append-to-body :close-on-click-modal="false">
      <el-form ref="detialsForm" :model="detialsForm" label-width="80px">
        <el-form-item label="消息标题" prop="noticeTitle">
          <el-input v-model="detialsForm.noticeTitle" placeholder="请输入消息标题" disabled />
        </el-form-item>
        <el-form-item label="消息内容" prop="noticeContent">
          <div v-html="detialsForm.noticeContent" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-select v-model="detialsForm.userId" multiple placeholder="请选择" disabled>
            <el-option v-for="item in memberListOptions" :key="item.userId" :label="item.label" :value="item.value" >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消息类型" prop="noticeType">
          <el-select v-model="detialsForm.noticeType" placeholder="请选择" disabled>
            <el-option v-for="item in noticeTypeOptions" :key="item.userId" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detialsOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMessage, delMessage, addMessage, updateMessage, memberList, getInfo } from "@/api/system/message";

export default {
  name: "Message",
  data() {
    return {
      loading: false, // 遮罩层
      submiting: false, // 提交中
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      messageList: [], // 站内消息 表格数据
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        params: {
          pageNum: 1,
          pageSize: 10,
        },
        noticeTitle: null,
        noticeType: null,
        status: null,
      },
      form: {}, // 表单参数
      // 表单校验
      rules: {
        noticeTitle: [{ required: true, message: "消息标题不能为空", trigger: "blur" }],
        noticeType: [{ required: true, message: "消息类型不能为空", trigger: "change" }],
      },
      //人员数据
      memberListOptions: [],
      //选中的值
      memberValue: '',
      //消息类型选项
      noticeTypeOptions: [
        { value: 1, label: '站内消息' },
        { value: 2, label: '系统通知' },
        { value: 3, label: '其他消息' },
      ],
      //详情对话框
      detialsOpen: false,
      //详情表单
      detialsForm: {}
    };
  },
  created() {
    this.getList();
    this.getMemberList();
  },
  methods: {
    /**打开查看详情弹窗 */
    async handleDetials(row) {
      let info = await getInfo({ noticeId: row.noticeId });
      this.detialsForm = info.data;
      this.detialsForm.userId = info.data.userId.split(',').map(item => item - 0);
      this.detialsOpen = true;
    },
    /**查询用户列表 */
    getMemberList() {
      memberList().then(res => {
        if (200 == res.code) {
          // this.memberListOptions = res.data;
          this.memberListOptions = res.data.reduce((data, item) => {
            return [
              ...data,
              {
                value: item.userId,
                label: item.nickName
              }
            ]
          }, []);
        }
      })
    },
    /** 查询站内消息 列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams)
        .then((res) => {
          this.messageList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: null,
        userId: null,
        noticeTitle: null,
        noticeType: null,
        noticeContent: null,
        status: null,
      };
      this.memberValue = null,
        this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = Object.assign({}, row);
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.noticeId) {
            console.log(this.form);
            // updateMessage(this.form).then((res) => {
            //   this.$modal.msgSuccess("修改成功");
            //   this.open = false;
            //   this.getList();
            // });
          } else {
            if (this.form.userIds?.length > 0) {
              this.form.isAll = 1;
            } else {
              this.form.isAll = 2;
            }
            console.log(this.form, '新增');
            this.submiting = true;
            addMessage(this.form).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(()=>{
              this.submiting = false;
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除名为【" + row.noticeTitle + "】的消息吗？")
        .then(() => {
          delMessage([row.noticeId]).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => { });
    },
  },
};
</script>
<style scoped lang="scss">
.el-select {
    width: 100%;
}
</style>
